package com.conversationboard.controller;

import java.io.IOException;
import java.security.Principal;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.conversationboard.model.User;

@WebServlet(name = "AskNewQuestionControllerServlet", urlPatterns = "/AskNewQuestionControllerServlet")
public class AskNewQuestionControllerServlet extends HttpServlet {

	private static final long serialVersionUID = 5732107337243840433L;


	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		try {
			request.setCharacterEncoding("UTF-8");
			response.setCharacterEncoding("UTF-8");

			String boardId = request.getParameter("boardid");

			Principal principal = request.getUserPrincipal();
			String userId = null;

			if (principal != null) {
				userId = principal.getName();
			}

			User user = User.get(userId);

			request.setAttribute("user", user);

			RequestDispatcher dispatcher = request.getRequestDispatcher("/Pages/asknewquestion.jsp?boardid=" + boardId);
			dispatcher.forward(request, response);

		} catch (SQLException e) {
			throw new ServletException(e);
		}

	}

}
